from pieces import *
import copy
from math import sqrt


class Board():
	
	def __init__(self):
		self.cells=[[('0')] * 8 for i in range(8)]
		self.white=[]
		self.black=[]
		
	def loadBoard(self):
		for p in self.white:
			pos=list(p.position)
			self.cells[pos[0]][pos[1]]=p.name
		
		for p in self.black:
			pos=list(p.position)
			self.cells[pos[0]][pos[1]]=p.name
		
		
	def printBoard(self):
		"Muestra el tablero en consola"		
		for x in range(8):
			for y in range(8):			
				print self.cells[x][y] + '  ' , 		
			print '\n'

	def friendlyFire(self, position, player):
		"""Indica si la posicion esta ocupada por una pieza del color indicado"""
		position=tuple(position)
		if(player=='W'):
			for p in self.white:
				if (p.position == position):
					return True
		else:
			for p in self.black:
				if (p.position == position):
					return True
		
		return False

	def addWhitePiece(self, p):
		self.white.append(copy.copy(p))
		
	def addBlackPiece(self, p):
		self.black.append(copy.copy(p))
		
	def getWhitePieces(self):
		return self.white

	def getBlackPieces(self):
		return self.black

	def getBlackKingPosition(self):
		for p in self.black:
			if(p.name=='K'):
				return p.position
				
	def getWhiteKingPosition(self):
		for p in self.white:
			if(p.name=='K'):
				return p.position			

	def thereIsOponentPiece(self,position,player):
		if(player=='W'):
			pieces=self.black
		else:
			pieces=self.white
		
		for p in pieces:
			if (p.position==position):
				return True
	
		return False
					
					
	def checkPawnDobleMov(self,position,player):				
		if( player=='W'):
			if position[0]!=6:
				return False
			aux=-1
		
		if(player=='B'):
			if position[0]!=1:
				return False		
			aux=1
			
			
		middleCell=[]
		middleCell.append(position[0]+aux)
		middleCell.append(position[1])	
		middleCell=tuple(middleCell)
		
		if(self.cellOccuped(middleCell)):
			return False
		
		return True
	
	def cellOccuped(self,cell):
		for p in self.white:
			if p.position==cell:
				return True
		
		for p in self.black:
			if p.position==cell:
				return True		
		
		return False			
					
	
	def getThreatenedCells(self,player):
		if(player=='W'):
			pieces=self.black
		else:
			pieces=self.white
			
		threatenedCells=[]
		
		for p in pieces:
			
			
			
			pos=list(p.position)
			auxPos=[]
			
			if(p.name=='P'):
				if(player=='W'):
					auxPos.append(pos[0]+1)
					auxPos.append(pos[1]-1)
					threatenedCells.append(tuple(auxPos))
					auxPos[1]=pos[1]+1
					threatenedCells.append(tuple(auxPos))
					continue
				else:
					auxPos.append(pos[0]-1)
					auxPos.append(pos[1]-1)
					threatenedCells.append(tuple(auxPos))
					auxPos[1]=pos[1]+1
					threatenedCells.append(tuple(auxPos))
					continue
				
			if(p.name=='N'):
				move=p.move()
				for m in move:
					m=list(m);
					auxPos=list(pos)
					auxPos[0]+=m[0]
					auxPos[1]+=m[1]
					if((auxPos[0]<8 ) and (auxPos[0] >-1) and (auxPos[1]< 8) and (auxPos[1]>-1) ):
						threatenedCells.append(tuple(auxPos))
					
			dirDiagonal=[[1,1],[1,-1],[-1,-1],[-1,1]]
			dirVertical=[[0,1],[1,0],[0,-1],[-1,0]]
			
			if(p.name=='Q' or p.name=='B'):
				
				for d in dirDiagonal:
					auxPos=list(pos)
					auxPos[0]+=d[0]
					auxPos[1]+=d[1]
					
					while((auxPos[0]<8 ) and (auxPos[0] >-1) and (auxPos[1]< 8) and (auxPos[1]>-1) ):
							threatenedCells.append(tuple(auxPos))
							if (self.cellOccuped(tuple(auxPos))):
								if(player=='W'):
									if not(self.getBlackKingPosition()==tuple(auxPos)):
										break
								else:
									if not(self.getWhiteKingPosition()==tuple(auxPos)):
										break
							auxPos[0]+=d[0]
							auxPos[1]+=d[1]
							
			if(p.name=='Q' or p.name=='R'):
				for d in dirVertical:
					auxPos=list(pos)
					auxPos[0]+=d[0]
					auxPos[1]+=d[1]
					
					while((auxPos[0]<8 ) and (auxPos[0] >-1) and (auxPos[1]< 8) and (auxPos[1]>-1) ):
							threatenedCells.append(tuple(auxPos))
							if (self.cellOccuped(tuple(auxPos))):
								if(player=='W'):
									if not(self.getWhiteKingPosition()==tuple(auxPos)):
										break
								else:
									if not(self.getBlackKingPosition()==tuple(auxPos)):
										break
									
							
							
							auxPos[0]+=d[0]
							auxPos[1]+=d[1]	
					
					
			if(p.name=='K'):
				for m in p.move():
					auxPos=list(pos)
					auxPos[0]+=m[0]
					auxPos[1]+=m[1]
					if((auxPos[0]<8 ) and (auxPos[0] >-1) and (auxPos[1]< 8) and (auxPos[1]>-1) ):
						threatenedCells.append(tuple(auxPos))
				
		
		return threatenedCells

	def isCrowning(self, player):
		
		if(player=='W'):
			for p in self.white:
				if(p.name=='P'):
					if(p.position[0]==0):
						self.addWhitePiece(Queen((p.position[0],p.position[1]),'Q'))
						self.white.remove(p)
						break 
		else:
			for p in self.black:
				if(p.name=='P'):
					if(p.position[0]==7):
						self.addBlackPiece(Queen((p.position[0],p.position[1]),'Q'))
						self.black.remove(p) 
						break		
